package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.EmsPeakShift;
import com.econ.powercloud.ems.dal.mapper.EmsPeakShiftMapper;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zjr
 * @create 2021/7/22 下午2:33
 * @description
 */
@Repository
public class EmsPeakShiftDAO extends ServiceImpl<EmsPeakShiftMapper, EmsPeakShift> {

    /**
     * 通过储能站ID与储能装置ID搜索列表
     * @param energyStationId
     * @param energyEquipmentId
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<EmsPeakShift> selectPageByStationIdByEquipmentId(String energyStationId, String energyEquipmentId, Integer pageNo, Integer pageSize) {
        LambdaQueryWrapper<EmsPeakShift> emsPeakShiftLambdaQueryWrapper = Wrappers.lambdaQuery();
        emsPeakShiftLambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(energyStationId), EmsPeakShift::getEnergyStationId, energyStationId);
        emsPeakShiftLambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(energyEquipmentId), EmsPeakShift::getEnergyEquipmentId, energyEquipmentId);
        Page<EmsPeakShift> emsPeakShiftPage = new Page<>(pageNo, pageSize);
        return this.baseMapper.selectPage(emsPeakShiftPage, emsPeakShiftLambdaQueryWrapper);
    }

    /**
     * 批量update策略启用状态
     */
    public void batchModifyEnableByStationIdAndEquipmentId(String stationId , String equipmentId , Integer peakShiftEnable){
        LambdaUpdateWrapper<EmsPeakShift> emsPeakShiftLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        emsPeakShiftLambdaUpdateWrapper.eq(EmsPeakShift::getEnergyStationId, stationId)
                .eq(EmsPeakShift::getEnergyEquipmentId,equipmentId).set(EmsPeakShift::getPeakShiftEnabled,peakShiftEnable);
        this.baseMapper.update(null,emsPeakShiftLambdaUpdateWrapper);

    }

    /**
     * 通过储能站id与储能装置id获取所有策略
     * @param energyStationId
     * @param energyEquipmentId
     */
    public List<EmsPeakShift> selectListByStationIdByEquipmentId(String energyStationId, String energyEquipmentId){
        LambdaQueryWrapper<EmsPeakShift> emsPeakShiftLambdaQueryWrapper = Wrappers.lambdaQuery();
        emsPeakShiftLambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(energyStationId), EmsPeakShift::getEnergyStationId, energyStationId);
        emsPeakShiftLambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(energyEquipmentId), EmsPeakShift::getEnergyEquipmentId, energyEquipmentId);
        return this.baseMapper.selectList(emsPeakShiftLambdaQueryWrapper);
    }

    public EmsPeakShift selectByStationIdAndEnable(String stationId, String equipmentId, Integer peakShiftEnable) {
        LambdaQueryWrapper<EmsPeakShift> wrapper = Wrappers.<EmsPeakShift>lambdaQuery()
                .eq(EmsPeakShift::getEnergyStationId, stationId).eq(EmsPeakShift::getEnergyEquipmentId, equipmentId)
                .eq(EmsPeakShift::getPeakShiftEnabled, peakShiftEnable);
        return this.baseMapper.selectOne(wrapper);
    }
}
